package servlet;

import entity.Field;
import entity.Operator;
import entity.Passage;
import service.CommonService;
import service.FieldService;
import service.OperatorService;
import service.PassageService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet( name = "PassageServlet")
public class PassageServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //使用到的services
        PassageService passageService = new PassageService();
        OperatorService operatorService = new OperatorService();
        CommonService commonService = new CommonService();

        //获取文章ID
        String passageId = request.getParameter("passageId");
        Passage passage = passageService.getPassageById(passageId);
        request.setAttribute("passage", passage);

        //根据文章获取Operator信息
        Operator operator = operatorService.getOperatorById(passage.getOperator().toString());
        request.setAttribute("operatorName", operator.getName());

        //获取课程名称
        String courseName = commonService.getCourseName();
        request.setAttribute("courseName", courseName);
        //获取底部信息
        Passage bottomPassage = passageService.getPassageByField("5");
        String bottom_content = (bottomPassage == null) ? "暂无" : bottomPassage.getContent();
        request.setAttribute("bottom_content", bottom_content);

        //获取field信息，用于定位
        String fieldId = request.getParameter("field");
        if (fieldId != null){
            FieldService fieldService = new FieldService();
            Field field = fieldService.getFieldById(fieldId);
            request.setAttribute("field", field);
        }
        request.getRequestDispatcher("passage.jsp").forward(request, response);
    }
}
